<template>
  <div class="login-container">
    <video src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" autoplay="autoplay" loop="loop" muted="muted" style="width:100% ; height:100%; object-fit: fill">.</video>
    <el-form ref="loginForm" v-model="loginForm" :model="loginForm" :rules="rules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <!-- <h3 class="title">Login Form</h3> -->
        <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="">
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code" class="code-image-box">
        <img :src="img" class="code-image" @click="changeCode">
        <el-input
          ref="username"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        class="login-button"
        @click.native="hLogin"
      >登录</el-button>

      <div class="tips">
        仅用于IT培训教学使用,为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！
      </div>

    </el-form>
    <div class="login-footer">
      <div
        style="
          font-size: 14px;
          color: white;
          text-align: center;
          position: absolute;
          bottom: 0;
          left: 0%;
          width: 100%;
          opacity: 0.9;
        "
      >
        江苏传智播客教育科技股份有限公司
        <span
          style="margin-left: 20px"
        >版权所有Copyright 2006-2022 All Rights Reserved</span>
        <span style="margin-left: 20px">苏ICP备16007882号-11</span>
      </div>
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { code } from '@/api/user'
import { randomNum } from '@/utils'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'shenlingadmin', // 用户名
        password: '123456', // 密码
        tenant: '0000',
        bindAccount: '',
        bindPassword: '',
        signAccount: '',
        signPassword: '',
        code: '',
        key: randomNum(24, 16)
      },
      rules: {
        account: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 1, message: '长度最少1个字符', trigger: 'blur' }

        ]
      },

      loading: false,
      passwordType: 'password',
      redirect: undefined,
      img: ''
    }
  },
  // 进入登录页就发送请求获取数据
  async created() {
    // 获取验证码
    this.getcode()
  },

  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 获取验证码函数
    async getcode() {
      const res = await code(this.loginForm.key)
      // console.log(res)
      console.log(res.data)
      this.img = window.URL.createObjectURL(new Blob([res.data]))
    },
    // 点击切换验证码
    changeCode() {
      this.getcode()
    },
    async doLogin() {
      console.log('发请求')
      await this.$store.dispatch('user/login', this.loginForm)
      // 跳转到主页
      this.$router.push('/')
    },
    async hLogin() {
      // 表单校验
      this.$refs.loginForm.validate((valid) => {
        valid && this.doLogin()
      })
      // const res = await request.post('/sys/login', {
      //   mobile: this.loginForm.mobile,
      //   password: this.loginForm.password
      // })
      // console.log(res.data)
      this.$message.success('登录成功')
    }

  }

}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#454545;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
     &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // width: 100%;
    border: 1px solid #dcdfe6;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    line-height: 40px;
    position: relative;
    font-size: 14px;
    margin-bottom: 20px;
    .el-input{
    position: relative;
    display: inline-block;
    // width: 100%;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    // color: #606266;
    font-size: inherit;
    height: 40px;
    // outline: 0;
    padding: 0 15px;
    }
  }
  .login-button{
    width: 100%;
    height: 54px;
    margin-bottom: 14px;
    margin-top: 22px;
    background-color: #e15536;
    border: 0;
    font-size: 18px;
    color: #fff;
    font-weight: 700
  }
  }
</style>

<style lang="scss" scoped>$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container{
  height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  video{
  display: inline-block;
  }
 .login-form {
    position: fixed;
    top: 45%;
    left: 45%;
    margin: -180px 0 0 -160px;
    width: 450px;
    height: 490px;
    padding: 36px 40px;
    background: #fff;
    border-radius: 10px;
    text-align: center;
    .title-container{
    position: relative;
    margin-bottom: 28px;
    text-align: center;

   img{
     height: 54px;
  }
     }
     }
.login-footer {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    color: white;
    font-size: 0.85rem;
    line-height: 1rem;
    height: 1rem;
  }
  .tips {
   line-height: 20px;
    font-size: 14px;
    color: #fd3333;
    margin-bottom: 20px;
    margin-top: -15px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .code-image-box {
      .code-image{
        display: inline-block;
        vertical-align: top;
        width: 71px;
        height: 32px;
        cursor: pointer;
        margin-left: 10px;
        position: absolute;
        right: 10px;
        top: 4px;
        background: #fff;
        z-index: 99;
        }
    }

}
</style>
